package top.tangyh.lamp.common.utils;

import cn.hutool.core.util.ObjectUtil;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import freemarker.template.utility.StringUtil;
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;
import top.tangyh.basic.exception.BizException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证工具
 */
public class ValidationUtil {

    /**
     * 验证空
     */
    public static void isNull(Object obj, String entity, String parameter , Object value){
        if(ObjectUtil.isNull(obj)){
            String msg = entity + " 不存在: "+ parameter +" is "+ value;
            throw new BizException(msg);
        }
    }

    /**
     * 验证是否为邮箱
     */
    public static boolean isEmail(String email) {
        return new EmailValidator().isValid(email, null);
    }


    private static Pattern p=Pattern.compile("[0-9]*");

    public static boolean isNumeric(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
//        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = p.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

}
